//
//  WHZViewController.swift
//  iiatn
//
//  Created by mine on 18/4/24.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit

class WHZViewController: UIViewController {
    
    @IBOutlet var mview: UIScrollView!
    
    @IBOutlet var kuai1: UIButton!
    
    @IBOutlet var kuai2: UIButton!
    
    @IBOutlet var kuai3: UIButton!
    
    @IBOutlet var kuai4: UIButton!
    
    @IBOutlet var more1: UIButton!
    
    @IBOutlet var more2: UIButton!
    
    @IBOutlet var more3: UIButton!
    
    @IBOutlet var more4: UIButton!
    
    @IBOutlet var image1: UIImageView!
    
    @IBOutlet var image2: UIImageView!
    
    @IBOutlet var image3: UIImageView!
    
    @IBOutlet var image4: UIImageView!
    
    @IBOutlet var image5: UIImageView!
    
    @IBOutlet var image6: UIImageView!
    
    @IBOutlet var image7: UIImageView!
    
    @IBOutlet var image8: UIImageView!
    
    @IBOutlet var lab1: UIButton!
    
    @IBOutlet var lab2: UIButton!
    
    @IBOutlet var lab3: UIButton!

    @IBOutlet var lab4: UIButton!
    
    @IBOutlet var lab5: UIButton!
    
    @IBOutlet var lab6: UIButton!
    
    @IBOutlet var lab7: UIButton!
    
    @IBOutlet var lab8: UIButton!
    
    @IBOutlet var btn1: UIButton!
    
    @IBOutlet var btn2: UIButton!
    
    @IBOutlet var btn3: UIButton!
    
    var kuais = [UIButton]()
    var mores = [UIButton]()
    var imgs = [UIImageView]()
    var labs = [UIButton]()
    var btns = [UIButton]()
    
    var zixuns = [WZiXun]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setdata()
        setstyle()
        loaddata()
        setdosth()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func setdata(){
        kuais.append(kuai1)
        kuais.append(kuai2)
        kuais.append(kuai3)
        kuais.append(kuai4)
        
        mores.append(more1)
        mores.append(more2)
        mores.append(more3)
        mores.append(more4)
        
        imgs.append(image1)
        imgs.append(image2)
        imgs.append(image3)
        imgs.append(image4)
        imgs.append(image5)
        imgs.append(image6)
        imgs.append(image7)
        imgs.append(image8)
        
        labs.append(lab1)
        labs.append(lab2)
        labs.append(lab3)
        labs.append(lab4)
        labs.append(lab5)
        labs.append(lab6)
        labs.append(lab7)
        labs.append(lab8)
        
        btns.append(btn1)
        btns.append(btn2)
        btns.append(btn3)
    }
    func setstyle(){
        mview.width = SCREENW
        mview.height = SCREENH
        mview.contentSize = CGSize(width: SCREENW, height: SCREENH)
        
        for kuai in kuais {
            kuai.layer.cornerRadius = kuai.height * 0.08
            kuai.layer.masksToBounds = true
            kuai.layer.rasterizationScale = UIScreen.main.scale
        }
        var morei = 0
        for more in mores {
            more.x = SCREENW - more.width - 20
            more.tag = 21 + morei
            morei += 1
        }
        var imgi = 0
        for img in imgs {
            if imgi%2 == 1 {
                img.x = SCREENW - 178
            }
            imgi += 1
        }
        var labi = 0
        for lab in labs {
            lab.tag = labi
            if labi%2 == 1 {
                lab.x = SCREENW - 178
            }
            labi += 1
        }
        for btn in btns {
            btn.width = SCREENW
        }
    }
    func loaddata(){
        for inde in 0...3 {
            WNetwork.shareNetworkTool.loadHBZixun(catid: 21+inde ){ (hbzixun2) in
                self.imgs[0+inde*2].kf.setImage(with: URL(string: hbzixun2[0].thumb!)!, placeholder: UIImage(named: "default_bg"), options: nil, progressBlock: nil)
                self.imgs[1+inde*2].kf.setImage(with: URL(string: hbzixun2[1].thumb!)!, placeholder: UIImage(named: "default_bg"), options: nil, progressBlock: nil)
                self.labs[0+inde*2].setTitle(hbzixun2[0].title!, for: .normal)
                self.labs[1+inde*2].setTitle(hbzixun2[1].title!, for: .normal)
                self.zixuns.append(hbzixun2[0])
                self.zixuns.append(hbzixun2[1])
            }
        }
    }
    func setdosth(){
        for more in mores {
            more.addTarget(self, action: #selector(golist), for: .touchUpInside)
        }
        for lab in labs {
            lab.addTarget(self, action: #selector(godetail), for: .touchUpInside)
        }
    }

    func golist(button: UIButton){
        let listVC = WZixunCidTableViewController()
        listVC.mcid = button.tag
        listVC.title = " "
        navigationController?.pushViewController(listVC, animated: true)
    }
    func godetail(button: UIButton){
        let detailVC = WZixunDetailViewController()
        detailVC.title = " "
        detailVC.zixunItem = zixuns[button.tag]
        navigationController?.pushViewController(detailVC, animated: true)
    }
}
